package o2o.web.actions.shop;

import com.jinyou.utils.common.*;
import com.jinyouapp.common.tools.jms.model.JMSNotify;
import o2o.cache.shop.ShopExtraInfoMemory;
import o2o.cache.shop.ShopMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.config.dao.UserDaoSql;
import o2o.config.jms.config.JMSTypeContent;
import o2o.config.jms.sender.JmsSender;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.common.UserType;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.shop.ShopInfoExtra;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.common.FileUploadInfo;
import o2o.dao.VO.shop.ShopInfoExtraVO;
import o2o.system.data.SYS_CONFIG_PATH;
import o2o.web.actions.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

//店铺额外信息
@Controller
public class ShopExtraAction {

    //获取店铺资质信息
    @RequestMapping(method = RequestMethod.POST, value = "/shop/license/get")
    @ResponseBody
    public String shop_Lic_get(String token, Long shopId) {
        //验证用户有效性
//        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
//        if (user == null) {
//            return AjaxResult.errorLogoutResult();
//        }

        ShopInfoExtra shopInfoExtra = ShopExtraInfoMemory.getInstance().getShopExtraInfoById(shopId);
        if (null != shopInfoExtra) {
            if (ValidateUtil.isNotNull(shopInfoExtra.getBuzzLicImageUrl()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getBuzzLicImageUrl())) {
//                shopInfoExtra.setBuzzLicImageUrl(Common.imageUrl + shopInfoExtra.getBuzzLicImageUrl());
                shopInfoExtra.setBuzzLicImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getBuzzLicImageUrl());
            }
            if (ValidateUtil.isNotNull(shopInfoExtra.getIDCardAImageUrl()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getIDCardAImageUrl())) {
//                shopInfoExtra.setIDCardAImageUrl(Common.imageUrl + shopInfoExtra.getIDCardAImageUrl());
                shopInfoExtra.setIDCardAImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getIDCardAImageUrl());
            }
            if (ValidateUtil.isNotNull(shopInfoExtra.getIDCardBImageUrl()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getIDCardBImageUrl())) {
//                shopInfoExtra.setIDCardBImageUrl(Common.imageUrl + shopInfoExtra.getIDCardBImageUrl());
                shopInfoExtra.setIDCardBImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getIDCardBImageUrl());
            }
            if (ValidateUtil.isNotNull(shopInfoExtra.getIDCardHImageUrl()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getIDCardHImageUrl())) {
//                shopInfoExtra.setIDCardHImageUrl(Common.imageUrl + shopInfoExtra.getIDCardHImageUrl());
                shopInfoExtra.setIDCardHImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getIDCardHImageUrl());
            }
            if (ValidateUtil.isNotNull(shopInfoExtra.getHealthLicImageUrl()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getHealthLicImageUrl())) {
//                shopInfoExtra.setHealthLicImageUrl(Common.imageUrl + shopInfoExtra.getHealthLicImageUrl());
                shopInfoExtra.setHealthLicImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getHealthLicImageUrl());
            }
            if (ValidateUtil.isNotNull(shopInfoExtra.getFoodLicImageUrl()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getFoodLicImageUrl())) {
//                shopInfoExtra.setFoodLicImageUrl(Common.imageUrl + shopInfoExtra.getFoodLicImageUrl());
                shopInfoExtra.setFoodLicImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getFoodLicImageUrl());
            }
            if (ValidateUtil.isNotNull(shopInfoExtra.getContractImageUrl()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getContractImageUrl())) {
//                shopInfoExtra.setFoodLicImageUrl(Common.imageUrl + shopInfoExtra.getFoodLicImageUrl());
                shopInfoExtra.setContractImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getContractImageUrl());
            }
            if (ValidateUtil.isNotNull(shopInfoExtra.getHealthCertImageUrl()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getHealthCertImageUrl())) {
//                shopInfoExtra.setFoodLicImageUrl(Common.imageUrl + shopInfoExtra.getFoodLicImageUrl());
                shopInfoExtra.setHealthCertImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getHealthCertImageUrl());
            }

            if (ValidateUtil.isNotNull(shopInfoExtra.getInstoreImageUrl()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getInstoreImageUrl())) {
//                shopInfoExtra.setFoodLicImageUrl(Common.imageUrl + shopInfoExtra.getFoodLicImageUrl());
                shopInfoExtra.setInstoreImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getInstoreImageUrl());
            }

            if (ValidateUtil.isNotNull(shopInfoExtra.getShopFrontImageUrl()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getShopFrontImageUrl())) {
//                shopInfoExtra.setFoodLicImageUrl(Common.imageUrl + shopInfoExtra.getFoodLicImageUrl());
                shopInfoExtra.setShopFrontImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getShopFrontImageUrl());
            }
            // 酒牌
            if (ValidateUtil.isNotNull(shopInfoExtra.getLiquorLicenseImageUrl()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getLiquorLicenseImageUrl())) {
//                shopInfoExtra.setFoodLicImageUrl(Common.imageUrl + shopInfoExtra.getFoodLicImageUrl());
                shopInfoExtra.setLiquorLicenseImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getLiquorLicenseImageUrl());
            }

            // 店铺二维码
            if (ValidateUtil.isNotNull(shopInfoExtra.getWxPayImg()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getWxPayImg())) {
                shopInfoExtra.setWxPayImg(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getWxPayImg());
            }
            if (ValidateUtil.isNotNull(shopInfoExtra.getAliPayImg()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getAliPayImg())) {
                shopInfoExtra.setAliPayImg(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getAliPayImg());
            }
            if (ValidateUtil.isNotNull(shopInfoExtra.getImg1()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getImg1())) {
                shopInfoExtra.setImg1(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getImg1());
            }
            if (ValidateUtil.isNotNull(shopInfoExtra.getImg2()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getImg2())) {
                shopInfoExtra.setImg2(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getImg2());
            }
            if (ValidateUtil.isNotNull(shopInfoExtra.getImg3()) && ValidateUtil.isNotNetUrl(shopInfoExtra.getImg3())) {
                shopInfoExtra.setImg3(SYS_CONFIG_PATH.IMAGE_BASE_URL + shopInfoExtra.getImg3());
            }
        }

        return AjaxResult.successResult(shopInfoExtra);
    }

    //上传店铺资质照片
    @RequestMapping(method = RequestMethod.POST, value = "/shop/license/upload")
    @ResponseBody
    public String shop_Lic(String token, Long shopId, FileUploadInfo fileUploadInfo) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("信息填写有误，请查证");
        }

        ShopInfo shopInfo = ShopMemory.getInstance().getShopById(shopId);
        if (null == shopInfo) {
            return AjaxResult.errorResult("店铺信息有误，请查证");
        }

        if (!User.hasShopAuth(user.getUsername(), user.getUserType())) {
            if (0 == UserType.BUSINESS_MAN - user.getUserType() && user.getUsername().equalsIgnoreCase(shopInfo.getBusinessUsername())) {
                //有权限
            } else {
                return AjaxResult.errorResult("您不是店主无权修改");
            }
        }

        if (null == fileUploadInfo) {
            return AjaxResult.errorResult("请先选择图片");
        }

        String IDCardAImageUrl = "";
        String IDCardBImageUrl = "";
        String IDCardHImageUrl = "";
        String buzzLicImageUrl = "";
        String healthLicImageUrl = "";
        String foodLicImageUrl = "";
        String contractImageUrl = "";
        String healthCertImageUrl = "";
        String shopFrontImageUrl = "";
        String InstoreImageUrl = "";
        String liquorLicenseImageUrl = ""; // 酒牌

        StringBuffer sb = new StringBuffer();
        sb.append("update ").append(PojoTables.SHOP_INFO_EXTRA).append(" set ");

        if (null != fileUploadInfo && null != fileUploadInfo.getImage1()) {//身份证正面
            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage1());
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage1());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" IDCardAImageUrl='").append(filePath).append("',");
            IDCardAImageUrl = filePath;
        }

        if (null != fileUploadInfo && null != fileUploadInfo.getImage2()) {//店主身份证照片反面
            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage2());
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage2());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" IDCardBImageUrl='").append(filePath).append("',");
            IDCardBImageUrl = filePath;
        }


        if (null != fileUploadInfo && null != fileUploadInfo.getImage3()) {//店主身份证照片手持
            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage3());
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage3());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" IDCardHImageUrl='").append(filePath).append("',");
            IDCardHImageUrl = filePath;
        }

        if (null != fileUploadInfo && null != fileUploadInfo.getImage4()) {//营业执照照片
            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage4());
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage4());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" buzzLicImageUrl='").append(filePath).append("',");
            buzzLicImageUrl = filePath;
        }

        if (null != fileUploadInfo && null != fileUploadInfo.getImage5()) {//卫生许可证照片
            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage5());
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage5());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" healthLicImageUrl='").append(filePath).append("',");
            healthLicImageUrl = filePath;
        }

        if (null != fileUploadInfo && null != fileUploadInfo.getImage6()) {//食品经营许可证
            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage6());
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage6());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" foodLicImageUrl='").append(filePath).append("',");
            foodLicImageUrl = filePath;
        }
        if (null != fileUploadInfo && null != fileUploadInfo.getImage7()) {//合同照片
            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage7());
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage7());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" contractImageUrl='").append(filePath).append("',");
            contractImageUrl = filePath;
        }

        if (null != fileUploadInfo && null != fileUploadInfo.getImage8()) {//健康照
            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage7());
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage8());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" healthCertImageUrl='").append(filePath).append("',");
            healthCertImageUrl = filePath;
        }
        if (null != fileUploadInfo && null != fileUploadInfo.getImage9()) {//门脸照
            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage7());
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage9());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" shopFrontImageUrl='").append(filePath).append("',");
            shopFrontImageUrl = filePath;
        }
        if (null != fileUploadInfo && null != fileUploadInfo.getImage10()) {//店内环境
            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage7());
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage10());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" InstoreImageUrl='").append(filePath).append("',");
            InstoreImageUrl = filePath;
        }

        if (null != fileUploadInfo && null != fileUploadInfo.getImage11()) {//酒牌
            //存储图片
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage11());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" liquorLicenseImageUrl='").append(filePath).append("',");
            liquorLicenseImageUrl = filePath;
        }

        sb.append(" updateUser= '").append(user.getUsername()).append("' where shopId = ").append(shopId).append(" and delFlag = 0");

        try {
            // 添加店铺备用信息
            ShopInfoExtra shopInfoExtra = ShopExtraInfoMemory.getInstance().getShopExtraInfoById(shopId);
            if (null == shopInfoExtra) {
                ShopMan.addShopExtraInfo(shopId,
                        shopInfo.getOwnUsername()
                        , InstoreImageUrl
                        , buzzLicImageUrl
                        , IDCardHImageUrl
                        , IDCardAImageUrl
                        , IDCardBImageUrl
                        , contractImageUrl
                        , healthLicImageUrl
                        , healthCertImageUrl
                        , foodLicImageUrl
                        , shopFrontImageUrl
                        , liquorLicenseImageUrl
                        , user.getUsername());
            } else {
                UserDaoSql.getInstance().executeBySql(sb.toString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("保存失败，请稍后再试");
        }

        ShopExtraInfoMemory.getInstance().addOrModify(shopId);

        return AjaxResult.successResult();
    }

    // 上传店铺其他图片
    @RequestMapping(method = RequestMethod.POST, value = "/shop/extra/img/upload")
    @ResponseBody
    public String shop_Lic_2(String token, Long shopId, FileUploadInfo fileUploadInfo) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("信息填写有误，请查证");
        }

        ShopInfo shopInfo = ShopMemory.getInstance().getShopById(shopId);
        if (null == shopInfo) {
            return AjaxResult.errorResult("店铺信息有误，请查证");
        }

        if (!User.hasShopAuth(user.getUsername(), user.getUserType())) {
            if (0 == UserType.BUSINESS_MAN - user.getUserType() && user.getUsername().equalsIgnoreCase(shopInfo.getBusinessUsername())) {
                //有权限
            } else {
                return AjaxResult.errorResult("您不是店主无权修改");
            }
        }

        if (null == fileUploadInfo) {
            return AjaxResult.errorResult("请先选择图片");
        }

        String wxPayImg = "";
        String aliPayImg = "";
        String img1 = "";
        String img2 = "";
        String img3 = "";


        StringBuffer sb = new StringBuffer();
        sb.append("update ").append(PojoTables.SHOP_INFO_EXTRA).append(" set ");

        if (null != fileUploadInfo && null != fileUploadInfo.getImage1()) {//身份证正面
            //存储图片
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage1());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" wxPayImg='").append(filePath).append("',");
            wxPayImg = filePath;
        }

        if (null != fileUploadInfo && null != fileUploadInfo.getImage2()) {//店主身份证照片反面
            //存储图片
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage2());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" aliPayImg='").append(filePath).append("',");
            aliPayImg = filePath;
        }


        if (null != fileUploadInfo && null != fileUploadInfo.getImage3()) {//店主身份证照片手持
            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage3());
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage3());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" img1='").append(filePath).append("',");
            img1 = filePath;
        }

        if (null != fileUploadInfo && null != fileUploadInfo.getImage4()) {//营业执照照片
            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage4());
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage4());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" img2='").append(filePath).append("',");
            img2 = filePath;
        }

        if (null != fileUploadInfo && null != fileUploadInfo.getImage5()) {//卫生许可证照片
            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage5());
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage5());
            if (null == filePath) {
                return AjaxResult.errorResult("存储图片失败，请稍后再试");
            }
            sb.append(" img3='").append(filePath).append("',");
            img3 = filePath;
        }


        sb.append(" updateUser= '").append(user.getUsername()).append("' where shopId = ").append(shopId).append(" and delFlag = 0");

        try {
            // 添加店铺备用信息
            ShopInfoExtra shopInfoExtra = ShopExtraInfoMemory.getInstance().getShopExtraInfoById(shopId);
            if (null == shopInfoExtra) {
                shopInfoExtra.setCreateTime(DateUtils.getCurrTime());
                shopInfoExtra.setDelFlag(0);
                shopInfoExtra.setCreateUser(user.getUsername());
                shopInfoExtra.setShopId(shopId);
                shopInfoExtra.setOwnUsername(shopInfo.getOwnUsername());
                shopInfoExtra.setWxPayImg(wxPayImg);
                shopInfoExtra.setAliPayImg(aliPayImg);
                shopInfoExtra.setImg1(img1);
                shopInfoExtra.setImg2(img2);
                shopInfoExtra.setImg3(img3);
                UserDaoSql.getInstance().saveObj(PojoTables.SHOP_INFO_EXTRA, shopInfoExtra);
            } else {
                UserDaoSql.getInstance().executeBySql(sb.toString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("保存失败，请稍后再试");
        }

        ShopExtraInfoMemory.getInstance().addOrModify(shopId);

        return AjaxResult.successResult();
    }


    //获取打印信息
    @RequestMapping(method = RequestMethod.POST, value = "/shop/print/get")
    @ResponseBody
    public String shop_print_get(String token, Long shopId) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        ShopInfoExtra shopInfoExtra = ShopExtraInfoMemory.getInstance().getShopExtraInfoById(shopId);
        ShopInfoExtraVO vo = new ShopInfoExtraVO();
        if (null == shopInfoExtra) {
            vo.setIsAutoPrint(0);
            vo.setPrintCount(1);
        } else {
            try {
                ObjectUtils.copyPropertiesExclude(shopInfoExtra, vo, null);
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.errorResult("获取失败，请稍后再试");
            }
        }

        return AjaxResult.successResult(vo);
    }

    //修改打印信息
    @RequestMapping(method = RequestMethod.POST, value = "/shop/print/modify")
    @ResponseBody
    public String shop_print(String token, Long shopId, String printNote, Integer printCount, Integer isAutoPrint) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (!User.hasShopAuth(user.getUsername(), user.getUserType())) {
            return AjaxResult.errorResult("您不是店主无权修改");
        }


        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("信息填写有误，请查证");
        }

        ShopInfo shopInfo = ShopMemory.getInstance().getShopById(shopId);
        if (null == shopInfo) {
            return AjaxResult.errorResult("您的店铺信息有误，请查证");
        }

        ShopInfoExtra extra = ShopExtraInfoMemory.getInstance().getShopExtraInfoById(shopId);
        if (null == extra) {
            if (null == printCount) {
                printCount = 1;
            }
            if (null == isAutoPrint) {
                isAutoPrint = 0;
            }

            extra = new ShopInfoExtra();
            extra.setShopId(shopId);
            extra.setOwnUsername(shopInfo.getOwnUsername());
            extra.setDelFlag(0);
            extra.setCreateUser(user.getUsername());
            extra.setCreateTime(DateUtils.getCurrTime());
            extra.setPrintCount(printCount);
            extra.setPrintNote(printNote);
            extra.setIsAutoPrint(isAutoPrint);

            try {
                UserDaoSql.getInstance().insertObj(PojoTables.SHOP_INFO_EXTRA, extra);
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("修改失败，请稍后再试");
            }
        } else {
            if (null == printCount) {
                printCount = extra.getPrintCount();
            }
            if (null == isAutoPrint) {
                isAutoPrint = extra.getIsAutoPrint();
            }

            if (null == printNote) {
                printNote = extra.getPrintNote();
            }

            StringBuffer sb = new StringBuffer();
            sb.append("update ").append(PojoTables.SHOP_INFO_EXTRA).append(" set ").append(" isAutoPrint=").append(isAutoPrint)
                    .append(",printNote='").append(printNote).append("',printCount=").append(printCount).append(",updateUser= '")
                    .append(user.getUsername()).append("' where shopId = ").append(shopId).append(" and delFlag = 0");

            try {
                UserDaoSql.getInstance().executeBySql(sb.toString());
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("保存失败，请稍后再试");
            }
        }

        //更新缓存
        ShopExtraInfoMemory.getInstance().addOrModify(shopId);


        // 修改店铺信息
        StringBuffer sb = new StringBuffer();
        sb.append("update ").append(PojoTables.ShopInfo).append(" set autoPrint = ").append(isAutoPrint).append(" where delFlag = 0 and id = ").append(shopId);
        try {
            UserDaoSql.getInstance().executeUpdateBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败，请稍后再试");
        }
        ShopMemory.getInstance().modifyById(shopId);
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.SHOP_INFO_UPDATE).setLong01(shopId);
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }
        return AjaxResult.successResult();
    }


}
